package com.yunfei.api.smrz;
import com.yunfei.api.smrz.bo.AppFileSimpleBo;
import com.yunfei.api.smrz.bo.ShenHeYiJianBo;
import com.yunfei.bo.hgtzz.HeGeTouZiZheRenZhengBo;
import com.yunfei.bo.hgtzz.HeGeTouZiZheRenZhengSaveParam;
import com.yunfei.bo.hgtzz.HeGeTouZiZheRenZhengUpdateParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.smrz.HeGeTouZiZheRenZhengParamBo;
import com.yunfei.bo.smrz.IdCardBo;
import com.yunfei.bo.smrz.IdCardType;
import com.yunfei.enums.ResultStatus;
import com.yunfei.enums.ShiMingRenZhengStatus;
import com.yunfei.mapper.aiiidb.HeGeTouZiZheRenZhengMapper;
import com.yunfei.mapper.aiiidb.ShenHeYiJianMapper;
import com.yunfei.mapper.aiiidb.ShiMingRenZhengMapper;
import com.yunfei.mapper.crm.HeGeTouZiZheRenZhengFileMapper;
import com.yunfei.mapper.crm.TblobtSorageMapper;
import com.yunfei.service.ShiMingRenZhengService;
import com.yunfei.utils.TokenUtil;
import com.yunfei.utils.procedure.HeGeTouZiZheRenZhengProcedure;
import com.yunfei.utils.procedure.ShiMingRenZhengProcedureReturnInfo;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 合格投资者认证接口
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = "合格投资者认证",tags = "合格投资者认证(理财师发起)")
@RestController
@RequestMapping("/api/licaishi/hgtzz")
public class HeGeTouZiZheRenZhengApiController {
    @Autowired
    private ShiMingRenZhengMapper shiMingRenZhengMapper;
    @Autowired
    private HeGeTouZiZheRenZhengMapper heGeTouZiZheRenZhengMapper;
    @Autowired
    private ShenHeYiJianMapper shenHeYiJianMapper;
    @Autowired
    private Environment env;
    @Autowired
    private TblobtSorageMapper tblobtSorageMapper;
    @Autowired
    private HeGeTouZiZheRenZhengFileMapper heGeTouZiZheRenZhengFileMapper;
    @Autowired
    private ShiMingRenZhengService shiMingRenZhengService;
    //页面初始化
    @GetMapping("/get")
    @ApiOperation(value = "获取所有证件类型",notes = "获取所有证件类型",position = 1,response = Object.class)
    @ResponseBody
    public Object getAllCardType(String cCustNo){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        List<IdCardType> idCardTypeList=shiMingRenZhengMapper.getCardType(null);//证件类型
        List<IdCardType> zhengMingType=heGeTouZiZheRenZhengMapper.getZhengMingType(null);//证明类型
        resultMap.put("cardType",idCardTypeList);
        resultMap.put("zhengMingType",zhengMingType);
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("cCustNo",cCustNo);
        IdCardBo cardBo= shiMingRenZhengMapper.getLookInfo(queryMap);
        resultMap.put("userInfo",cardBo);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultMap);
        return resultModel;
    }
    //查询列表
    @GetMapping("/list")
    @ApiOperation(value = "查询列表",notes = "查询列表",position = 1,response = Object.class)
    @ResponseBody
    public Object getList(String cCustNo){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("cCustNo",cCustNo);
        List<HeGeTouZiZheRenZhengBo> resultList=heGeTouZiZheRenZhengMapper.getList(queryMap);//查询列表
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultList);
        return resultModel;
    }
    //查询
    @GetMapping("/edite")
    @ApiOperation(value = "查看",notes = "查看",position = 1,response = Object.class)
    @ResponseBody
    public Object edite(String lSerialNo,String cCustNo){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        if(StringUtil.isNullOrEmpty(lSerialNo)||"undefined".equals(lSerialNo)){
            Map<String,Object> lserialNoMap=new HashMap<>();
            lserialNoMap.put("cCustNo",cCustNo);
            lSerialNo=heGeTouZiZheRenZhengMapper.getLserialNo(lserialNoMap);
        }
        queryMap.put("lSerialNo",lSerialNo);

        try{
            HeGeTouZiZheRenZhengBo result=heGeTouZiZheRenZhengMapper.edite(queryMap);//获取单个值
            resultMap.put("result",result);
            //获取审核意见
            Map<String,Object> shenHeQueryMap=new HashMap<>();
            shenHeQueryMap.put("lSerialno",lSerialNo);
            List<ShenHeYiJianBo> shenHeYiJianBoList=shenHeYiJianMapper.hgtzzShenHeYiJianlist(shenHeQueryMap);
            resultMap.put("shenHeYiJian",shenHeYiJianBoList);
            Map<String,Object> fileQueryMap=new HashMap<>();
            fileQueryMap.put("cCustNo",cCustNo);
            fileQueryMap.put("lSerialNo",lSerialNo);
            List<AppFileSimpleBo> fileList=heGeTouZiZheRenZhengMapper.getFileList(fileQueryMap);
            resultMap.put("resultFile",fileList);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(resultMap);
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultModel;
    }
    //录入
    @PostMapping(value = "/save",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "录入",notes = "合格投资者认证(理财师发起)",position = 1,response = Object.class)
    @ResponseBody
    public Object save(HeGeTouZiZheRenZhengSaveParam param, HttpServletRequest request){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        //参数格式校验
        String cCustNo=param.getCustNo();//客户编号
        String zhengMingType=param.getZhengMingType();//证明类型
        String money=param.getMoney();//证明金额
        if(StringUtil.isNullOrEmpty(zhengMingType)){
            resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGLEIXING_NOTNULL_ERRPR.getCode());
            resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGLEIXING_NOTNULL_ERRPR.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(money)){
            resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGMONEY_NOTNULL_ERRPR.getCode());
            resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGMONEY_NOTNULL_ERRPR.getMessage());
            return resultModel;
        }
        //校验附件必填
        String imgLength=param.getImgLength();
        if(!StringUtil.isNullOrEmpty(imgLength)){
            int count=Integer.parseInt(imgLength);
            if(count==0){
                resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getCode());
                return resultModel;
            }
        }else{
            resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getMessage());
            resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getCode());
            return resultModel;
        }
        HeGeTouZiZheRenZhengParamBo heGeTouZiZheRenZhengParamBo=new HeGeTouZiZheRenZhengParamBo();
        heGeTouZiZheRenZhengParamBo.setpCustno(cCustNo);
        heGeTouZiZheRenZhengParamBo.setpProvetype(zhengMingType);
        heGeTouZiZheRenZhengParamBo.setpProvebalance(money);
        Connection con =shiMingRenZhengService.getConnection();
        HeGeTouZiZheRenZhengProcedure heGeTouZiZheRenZhengProcedure=new HeGeTouZiZheRenZhengProcedure();
        ShiMingRenZhengProcedureReturnInfo returnInfo= heGeTouZiZheRenZhengProcedure.save(heGeTouZiZheRenZhengParamBo,con);
       // shiMingRenZhengService.updateSmrzStatus(resultModel, cCustNo,userName);
        String reasonCode=returnInfo.getpReasonCode();//返回结果编码0失败1成功
        String reasonInfo=returnInfo.getpReasonInfo();//返回成功时未附件输出编号，返回失败时为错误提示信息
        queryMap.put("smrzResult",returnInfo);
        if("1".equals(reasonCode)){
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(reasonInfo);
        }else{
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(reasonInfo);
        }

        return resultModel;
    }
    //编辑保存
    //录入
    @PostMapping(value = "/update",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "编辑保存",notes = "合格投资者认证(理财师发起)",position = 1,response = Object.class)
    @ResponseBody
    public Object update(HeGeTouZiZheRenZhengUpdateParam param, HttpServletRequest request){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        //参数格式校验
        String cCustNo=param.getCustNo();//客户编号
        String zhengMingType=param.getZhengMingType();//证明类型
        String money=param.getMoney();//证明金额
        Map<String,Object> queryIdMap=new HashMap<>();
        queryIdMap.put("cCustNo",cCustNo);
        String id= heGeTouZiZheRenZhengMapper.getId(queryIdMap);
        if(StringUtil.isNullOrEmpty(id)){
            id="";
        }
       // param.setlSerialno(id);
        heGeTouZiZheRenZhengMapper.update(param);
        Map<String,Object> deleteMap=new HashMap<>();
        deleteMap.put("lSerialno",id);
        heGeTouZiZheRenZhengMapper.deleteOldFile(deleteMap);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(queryMap);
        return resultModel;
    }
    //查询
    @GetMapping("/getCrmFileList")
    @ApiOperation(value = "获取crm附件",notes = "获取crm附件",position = 1,response = Object.class)
    @ResponseBody
    public Object getCrmFileList(String lSerialNo,String cCustNo){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        try{
            Map<String,Object> fileQueryMap=new HashMap<>();
            fileQueryMap.put("cCustNo",cCustNo);
            fileQueryMap.put("lSerialNo",lSerialNo);
            List<AppFileSimpleBo> fileList=heGeTouZiZheRenZhengMapper.getCrmFileList(fileQueryMap);
            resultMap.put("resultFile",fileList);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(resultMap);
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultModel;
    }
}
